package com.sgb.kjwl.viewmodel.self.authentication


import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.sgb.kjwl.api.service.UserAPI
import com.sgb.kjwl.model.entity.self.OrganizationAuthEntity
import com.sgb.kjwl.model.entity.user.AddressEntity
import com.sgb.kjwl.model.entity.user.AuthPhotoEntity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import com.swgk.core.util.PickSelectEntity
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.io.File


class OrganizationAuthModel : BaseViewModel() {

    //识别照片内容的返回体
    val authPhotoEntity = MutableLiveData<AuthPhotoEntity>()

    //所有地址
    var mAllAddress = MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>()

    //结构类型
    var mOrganTypes = MutableLiveData<ArrayList<PickSelectEntity<*>>>()

    //认证详情
    var mOrganizationEntity  = MutableLiveData<OrganizationAuthEntity>()

    //上传组织执照
    fun upLicenseModel(context: Context, file : File){
        DialogHelper.showProgressDialog(context, null, "文件上传中...", 0, false, null).
        setCanceledOnTouchOutside(false)
        //创建map集合
        val photos: MutableMap<String, RequestBody> = HashMap()
        //将文件转换成多媒体文件，方便上传
        val requestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        photos["file" + "\"; filename=\"" + file.name + "image.jpg\""] = requestBody
        //接口请求
        APIManager.getInstance().uploadAPI.uploadPhoto(photos).enqueue(object : ICallBack<BaseEntity<AuthPhotoEntity>>(){
            override fun onSuccess(data: BaseEntity<AuthPhotoEntity>?) {
                DialogHelper.dismissProgressDialog()
                if (data == null || data.data == null) {
                    MToast.showToast(context, "上传失败")
                    authPhotoEntity.postValue(null)
                    return
                }


                authPhotoEntity.postValue(data.data)
            }

            override fun onFail() {
                super.onFail()
                authPhotoEntity.postValue(null)
            }
        })
    }

    /**
     * 所有地址选项
     */
    fun getAddressModel(context: Context?) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(true)
        APIManager.getInstance().goodsAPI.cityList.enqueue(object : ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>>(){
            override fun onSuccess(data: BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>?) {
                if (data != null && data.data != null){
                    mAllAddress.postValue(data.data as List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>)
                }
            }

        })
    }

    /**
     * 提交、编辑组织认证
     */
    fun saveOrUpdateOrganization(context: Context, entity : OrganizationAuthEntity){
        DialogHelper.showProgressDialog(context, null, "数据提交中...", 0, false, null)
            .setCanceledOnTouchOutside(true)
        APIManager.getInstance().userAPI.saveOrUpdateOrganization(UserAPI.UserCenterUrl.URL_SAVE_OR_UPDATE_ORGANIZATION, entity).enqueue(object :
            ICallBack<BaseEntity<*>>() {
            override fun onSuccess(data: BaseEntity<*>?) {
                DialogHelper.dismissProgressDialog()
                if (data == null){
                    return
                }
                message.postValue("提交成功")
            }

        })
    }

    /**
     * 获取组织机构类型
     */
    fun getOrganizationTypes(context: Context){
        DialogHelper.showProgressDialog(context, null, "加载中...", 0, false, null)
            .setCanceledOnTouchOutside(true)
        val map = HashMap<String, Any>()
        map["limit"] = 20
        map["page"] = 1
        map["name"] = ""
        APIManager.getInstance().userAPI.getOrganizationTypes(UserAPI.UserCenterUrl.URL_ORGANIZATION_TYPES, map).enqueue(object :
            ICallBack<BaseEntity<List<PickSelectEntity<*>>>>() {
            override fun onSuccess(data: BaseEntity<List<PickSelectEntity<*>>>?) {
                DialogHelper.dismissProgressDialog()
               if (data != null && data.data != null){
                   mOrganTypes.postValue(data.data as ArrayList<PickSelectEntity<*>>)
               }
            }
        })
    }

    /**
     * 根据组织no查询详情
     */
    fun getOrganizationDetailByNo(context: Context, no : String){
        DialogHelper.showProgressDialog(context, null, "加载中...", 0, false, null)
            .setCanceledOnTouchOutside(true)
        APIManager.getInstance().userAPI.getOrganizationDetailByNo(UserAPI.UserCenterUrl.URL_ORGANIZATION_DETAIL_BYNO, no).enqueue(object :
            ICallBack<BaseEntity<OrganizationAuthEntity>>() {
            override fun onSuccess(data: BaseEntity<OrganizationAuthEntity>?) {
                DialogHelper.dismissProgressDialog()
                if (data != null && data.data != null){
                    mOrganizationEntity.postValue(data.data)
                }
            }
        })
    }

}